package com.commerceerp.system.service;


import com.commerceerp.system.entity.SysUser;
import com.commerceerp.system.entity.Token;
import com.commerceerp.system.mapper.LoginMapper;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.MD5Tools;
import com.commerceerp.system.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/5/13/013.
 */
@Service
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    //登录请求
    public SysUser doLogin(String loginname, String password) {
        //查询用户
        SysUser sysUser = loginMapper.getUser(loginname,password);
        if(sysUser != null){
            loginMapper.updateLoginTime(sysUser.getId());
        }
        return sysUser;
    }

    //保存在数据库
    public void saveToken(String token,String userId) {
        //删除之前的token
        loginMapper.delToken(userId);
        String date = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),1);
        loginMapper.saveToken(new Token(token,userId,date));
    }

    //保存在数据库
    public ApiResult checkToken(String token) {
        Token to = loginMapper.getToken(token);
        if(to != null){
            if(StringUtils.equals("1",to.getDelFlag())){
                //删除之前的token
                loginMapper.deleteToken(to.getUserId());
                return  new ApiResult(400,"已经在其他地方登录！");
            }
            String date = DateUtil.getYMDHMSDate();
            if(DateUtil.compareDate(date,to.getDate())){//有效
                //修改token有效时间
                loginMapper.updToken(token,DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),1));
                return  new ApiResult(to.getUserId());
            }
            //删除之前的token
            loginMapper.deleteToken(to.getUserId());
            return  new ApiResult(400,"token过期");
        }
        return new ApiResult(400,"token无效");
    }

    //注册
    public ApiResult register(String loginname, String password, String phone) {
        //检验登录名是否存在
        if(loginMapper.checkLoginname(loginname) > 0){
            return new ApiResult(405,"用户名已存在");
        }
        //检验手机号是否存在
        if(loginMapper.checkPhone(phone) > 0){
            return new ApiResult(405,"手机号已存在");
        }
        //密码进行加密处理  明文为  password
        String pawDES = MD5Tools.getSHA256StrJava(password);
        SysUser sysUser = new SysUser(loginname,pawDES,phone);
        sysUser.insert();
        //注册
        if(loginMapper.register(sysUser) > 0){
            return new ApiResult(200,"注册成功");
        }
        return new ApiResult(405,"注册失败");
    }

    //退出登录
    public ApiResult logout(String token) {
        Token to = loginMapper.getToken(token);
        if(to != null){
            //删除之前的token
            loginMapper.deleteToken(to.getUserId());
            return  new ApiResult(200,"退出登录");
        }
        return new ApiResult(400,"token无效");
    }
}
